package logic

import (
	"context"
	"month2/C/api_C/pkg"
	"month2/C/rpc_C/order"

	"month2/C/api_C/internal/svc"
	"month2/C/api_C/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginRequest) (resp *types.Response, err error) {
	login, err := l.svcCtx.CRpc.Login(l.ctx, &order.LoginRequest{
		Tel:  req.Tel,
		Code: req.Code,
	})
	if err != nil {
		return &types.Response{
			Msg:  "登录失败",
			Data: nil,
			Code: 500,
		}, nil
	}
	token, err := pkg.GetJwtToken(login.Id)
	if err != nil {
		return nil, err
	}
	return &types.Response{
		Msg:  "登录成功",
		Data: token,
		Code: 200,
	}, nil
}
